﻿package mortal.game.view.wizard.panel
{
    import Message.Public.*;
    import com.mui.controls.*;
    import mortal.common.*;
    import mortal.common.global.*;
    import mortal.game.cache.*;
    import mortal.game.cache.packCache.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.scene3D.player.info.*;
    import mortal.game.utils.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.util.*;
    import mortal.game.view.palyer.*;
    import mortal.game.view.palyer.data.*;
    import mortal.game.view.wizard.data.*;
    import mortal.mvc.core.*;

    public class OtherEquipItem extends PlayerEquipItem
    {
        protected var _stateIcon:GImageBitmap;
        protected var _midTxt:GTextFiled;
        private var _group:int;
        private var _category:int;
        private var _type:int;
        private var _packType:int;
        private var _effect:int;
        private var _effectEx:int;
        private var _targetLevel:int;

        public function OtherEquipItem()
        {
            return;
        }// end function

        override protected function updateView() : void
        {
            super.updateView();
            if (this._midTxt)
            {
                this._midTxt.x = (this.width - this._midTxt.width) / 2;
                this._midTxt.y = (this.height - this._midTxt.height + _paddingTop) / 2;
            }
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            if (this._stateIcon)
            {
                this._stateIcon.dispose(param1);
                this._stateIcon = null;
            }
            if (this._midTxt)
            {
                this._midTxt.dispose(param1);
                this._midTxt = null;
            }
            this.removeListener();
            super.disposeImpl(param1);
            return;
        }// end function

        public function get midLabel() : String
        {
            if (this._midTxt)
            {
                return this._midTxt.text;
            }
            return "";
        }// end function

        public function set midLabel(param1:String) : void
        {
            var _loc_2:GTextFormat = null;
            if (param1)
            {
                if (this._midTxt == null)
                {
                    _loc_2 = GlobalStyle.textFormatHui.center();
                    this._midTxt = UIFactory.gTextField("", 0, 0, 50, 20, _middleLayer, _loc_2);
                    this._midTxt.mouseEnabled = false;
                    this._midTxt.multiline = true;
                    this._midTxt.wordWrap = true;
                    this._midTxt.width = width - _paddingLeft * 2;
                    this._midTxt.height = height - _paddingTop;
                }
                this._midTxt.htmlText = param1;
                this._midTxt.x = (this.width - this._midTxt.width) / 2;
                this._midTxt.y = (this.height - this._midTxt.height + _paddingTop) / 2;
            }
            else if (this._midTxt)
            {
                this._midTxt.dispose();
                this._midTxt = null;
            }
            return;
        }// end function

        override public function set itemData(param1:ItemData) : void
        {
            super.itemData = param1;
            if (_itemData)
            {
                this.isPrescritime = ItemsUtil.isPrescriptionItem(param1);
            }
            else
            {
                this.isPrescritime = false;
            }
            return;
        }// end function

        protected function resetStateIconPos() : void
        {
            if (isDisposed)
            {
                return;
            }
            if (this._stateIcon)
            {
                this._stateIcon.x = (this.width - this._stateIcon.width) * 0.5;
                this._stateIcon.y = (this.height - this._stateIcon.height) * 0.5;
                _topLayer.addChild(this._stateIcon);
            }
            return;
        }// end function

        public function setState(param1:int = 0, param2:String = "otherEquipUpdateState_") : void
        {
            if (param1 > 0)
            {
                if (this._stateIcon == null)
                {
                    this._stateIcon = UIFactory.gImageBitmap("", _paddingLeft, _paddingTop, null, this.resetStateIconPos);
                }
                this._stateIcon.imgUrl = param2 + param1 + ".png";
            }
            else if (this._stateIcon)
            {
                this._stateIcon.dispose();
                this._stateIcon = null;
            }
            return;
        }// end function

        protected function checkCanEquip(param1:Object = null) : void
        {
            var _loc_3:ItemData = null;
            var _loc_4:ItemData = null;
            var _loc_5:PackPosTypeCache = null;
            var _loc_6:ItemData = null;
            var _loc_2:* = Cache.instance.pack.backPackCache.getItemsByGroupCategoryAndType(this._group, this._category, this._type, this._effect, -1, -1, this._effectEx);
            if (_loc_2 && _loc_2.length)
            {
                for each (_loc_4 in _loc_2)
                {
                    
                    if (this._targetLevel == 0 || this._targetLevel >= _loc_4.itemInfo.level)
                    {
                        if (ItemsUtil.isPrescriOutTime(_loc_4))
                        {
                            continue;
                        }
                        if (_loc_3 == null || EquipmentUtil.getEquipBaseCombat(_loc_3) < EquipmentUtil.getEquipBaseCombat(_loc_4))
                        {
                            _loc_3 = _loc_4;
                        }
                    }
                }
                if (_loc_3)
                {
                    _loc_5 = Cache.instance.pack.getPackChacheByPosType(this._packType);
                    if (_loc_5)
                    {
                        _loc_6 = _loc_5.getItemDataByType(this._type);
                    }
                    if (_loc_6)
                    {
                        if (EquipmentUtil.getEquipBaseCombat(_loc_3) > EquipmentUtil.getEquipBaseCombat(_loc_6))
                        {
                            this.setState(2);
                        }
                        else
                        {
                            this.setState(0);
                        }
                    }
                    else
                    {
                        this.setState(1);
                    }
                }
                else
                {
                    this.setState(0);
                }
            }
            else
            {
                this.setState(0);
            }
            return;
        }// end function

        protected function updateLevel(param1:Object = null) : void
        {
            var _loc_2:WizardData = null;
            var _loc_3:FashionAdvanceData = null;
            if (this._packType == EPlayerItemPosType._EPlayerItemPosTypeRole)
            {
                this._targetLevel = Cache.instance.role.entityInfo.level;
            }
            else if (this._packType == EPlayerItemPosType._EPlayerItemPosTypeMount)
            {
                this._targetLevel = Cache.instance.mount.mountData.mountLevel;
            }
            else if (this._packType == EPlayerItemPosType._EPlayerItemPosTypePet)
            {
                this._targetLevel = Cache.instance.newPet.pet.code;
            }
            else if (this._packType > EPlayerItemPosType._EPlayerItemPosTypeSprite && this._packType < EPlayerItemPosType._EPlayerItemPosTypeSpriteEnd)
            {
                _loc_2 = Cache.instance.wizard.getWizardDataByCode(this._effectEx);
                if (_loc_2.isHasWizard)
                {
                    this._targetLevel = _loc_2.spriteInfo.SpriteLevel;
                }
                else
                {
                    this._targetLevel = -1;
                }
            }
            else if (this._packType > EPlayerItemPosType._EPlayerItemPosTypeFashion && this._packType < EPlayerItemPosType._EPlayerItemPosTypeFashionEnd)
            {
                _loc_3 = Cache.instance.fashion.getFashionInfo(GameController.playerSystem.fashion.getCurrFashionType());
                if (_loc_3.advanceInfo && _loc_3.advanceInfo.code > 0)
                {
                    this._targetLevel = _loc_3.advanceInfo.code;
                }
                else
                {
                    this._targetLevel = -1;
                }
            }
            this.checkCanEquip();
            return;
        }// end function

        protected function addListener() : void
        {
            NetDispatcher.addCmdListener(ServerCommand.BackPackItemsChange, this.checkCanEquip);
            if (this._packType == EPlayerItemPosType._EPlayerItemPosTypeRole)
            {
                Cache.instance.role.roleEntityInfo.addEventListener(EntityInfoEventName.UpdateLevel, this.updateLevel);
            }
            else if (this._packType == EPlayerItemPosType._EPlayerItemPosTypeMount)
            {
                NetDispatcher.addCmdListener(ServerCommand.MountLevelUp, this.updateLevel);
            }
            else if (this._packType == EPlayerItemPosType._EPlayerItemPosTypePet)
            {
                NetDispatcher.addCmdListener(ServerCommand.PetAdvanceBack, this.updateLevel);
            }
            else if (this._packType > EPlayerItemPosType._EPlayerItemPosTypeSprite && this._packType < EPlayerItemPosType._EPlayerItemPosTypeSpriteEnd)
            {
                NetDispatcher.addCmdListener(ServerCommand.WizardLevelUp, this.updateLevel);
                NetDispatcher.addCmdListener(ServerCommand.WizardActive, this.updateLevel);
            }
            else if (this._packType > EPlayerItemPosType._EPlayerItemPosTypeFashion && this._packType < EPlayerItemPosType._EPlayerItemPosTypeFashionEnd)
            {
                NetDispatcher.addCmdListener(ServerCommand.FashionLevelUp, this.updateLevel);
            }
            return;
        }// end function

        protected function removeListener() : void
        {
            NetDispatcher.removeCmdListener(ServerCommand.BackPackItemsChange, this.checkCanEquip);
            if (this._packType == EPlayerItemPosType._EPlayerItemPosTypeRole)
            {
                Cache.instance.role.roleEntityInfo.removeEventListener(EntityInfoEventName.UpdateLevel, this.updateLevel);
            }
            else if (this._packType == EPlayerItemPosType._EPlayerItemPosTypeMount)
            {
                NetDispatcher.removeCmdListener(ServerCommand.MountLevelUp, this.updateLevel);
            }
            else if (this._packType == EPlayerItemPosType._EPlayerItemPosTypePet)
            {
                NetDispatcher.removeCmdListener(ServerCommand.PetAdvanceBack, this.updateLevel);
            }
            else if (this._packType > EPlayerItemPosType._EPlayerItemPosTypeSprite && this._packType < EPlayerItemPosType._EPlayerItemPosTypeSpriteEnd)
            {
                NetDispatcher.removeCmdListener(ServerCommand.WizardLevelUp, this.updateLevel);
                NetDispatcher.removeCmdListener(ServerCommand.WizardActive, this.updateLevel);
            }
            else if (this._packType > EPlayerItemPosType._EPlayerItemPosTypeFashion && this._packType < EPlayerItemPosType._EPlayerItemPosTypeFashionEnd)
            {
                NetDispatcher.removeCmdListener(ServerCommand.FashionLevelUp, this.updateLevel);
            }
            return;
        }// end function

        public function isAutoUpdateEquip(param1:Boolean, param2:int, param3:int, param4:int, param5:int, param6:int = -1, param7:int = -1) : void
        {
            if (param1)
            {
                this._group = param2;
                this._category = param3;
                this._type = param4;
                this._packType = param5;
                this._effect = param6;
                this._effectEx = param7;
                this.addListener();
                this.updateLevel();
            }
            else
            {
                this.removeListener();
                this.setState(0);
            }
            return;
        }// end function

    }
}
